package tju.mic.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Set;


/**
 *
 * @author Snow
 */
public class FileUtils {

    public static Set<Class<?>> scanPackage(String packageName) throws Exception {
        ClassPathScanHandler handler = new ClassPathScanHandler(true, true, null);
        Set<Class<?>> clazzList = handler.getPackageAllClasses(
                packageName, false);
        return clazzList;
    }

    public static Set<Class<?>> scanPackageRecursively(String packageName, boolean recursive) throws Exception {
        ClassPathScanHandler handler = new ClassPathScanHandler(true, true, null);
        Set<Class<?>> clazzList = handler.getPackageAllClasses(
                packageName, recursive);
        return clazzList;
    }

    public static Set<Class<?>> scanPackageWithFilters(String packageName, List<String> filters) throws Exception {
        ClassPathScanHandler handler = new ClassPathScanHandler(true, true, filters);
        Set<Class<?>> clazzList = handler.getPackageAllClasses(
                packageName, false);
        return clazzList;
    }

    public static Set<Class<?>> scanPackageWithFilters(String packageName, List<String> filters, boolean recursive) throws Exception {
        ClassPathScanHandler handler = new ClassPathScanHandler(true, true, filters);
        Set<Class<?>> clazzList = handler.getPackageAllClasses(
                packageName, recursive);
        return clazzList;
    }

    public static void deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            file.delete();
        }
    }

    public static boolean saveLocalFile(String fileName, InputStream inputStream) throws Exception {
        boolean ret = false;
        try {
            OutputStream o = new FileOutputStream(fileName);
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while ((bytesRead = inputStream.read(buffer, 0, 1024)) != -1) {
                o.write(buffer, 0, bytesRead);
            }
            o.close();
            ret = true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ret = false;
        }
        return ret;
    }
    
    
    
    
}
